#include "GameState.h"
#include "CCEditBox.h"
using namespace cocos2d;
#include "DebugState.h"

CCScene* GameState::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    GameState *layer = GameState::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool GameState::init()
{
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
  
	MainMenuUI();

	m_pGameLogic = 0;

    return true;
}


void GameState::menuShopCallback( CCObject* pSender )
{
	//CCFadeOut* fo = CCFadeOut::create(0.5f);
	//CCSequence* seq = CCSequence::create(fo,NULL);
	//COCOUISYSTEM->getCurScene()->getRootWidget()->runAction(seq);
}

void GameState::menuStartGameCallback( CCObject* pSender )
{

	if(m_pGameLogic == 0)
		delete m_pGameLogic;
	m_pGameLogic = new GameLogic();
	m_pGameLogic->SetParentSceen(this);
	m_pGameLogic->InitLevelData(GameData::GetIns()->GetCurrentRole()->mIconIndex);
	m_pGameLogic->SetCallBack(this);
	GameUI();

}

void GameState::MainMenuUI()
{
	COCOUISYSTEM->resetSystem(this);    
	COCOUISYSTEM->getCurScene()->addWidget(COCOUISYSTEM->createWidgetFromFileWithAdapt_json("UIMainMenu.ExportJson", true, true));

	cs::CocoTextButton* bt = (cs::CocoTextButton*)(COCOUISYSTEM->getWidgetByName("btnShop"));
	bt->addReleaseEvent(this, coco_releaseselector(GameState::menuShopCallback));

	bt = (cs::CocoTextButton*)(COCOUISYSTEM->getWidgetByName("btnStart"));
	bt->addReleaseEvent(this, coco_releaseselector(GameState::menuStartGameCallback));
}

void GameState::GameUI()
{
	COCOUISYSTEM->resetSystem(this);   
	COCOUISYSTEM->getCurScene()->addWidget(COCOUISYSTEM->createWidgetFromFileWithAdapt_json("UIGame.ExportJson", true, true));

	
	for (int i = 0;i < MAX_COLOR_SLOT;i++)
	{
		cs::CocoButton* bt = (cs::CocoButton*)(COCOUISYSTEM->getWidgetByName((std::string("btnColor")+std::string(GetString(i+1))).c_str()));
		if(bt){
			bt->addReleaseEvent(this, coco_releaseselector(GameState::menuColorBtnCallback));
			bt->setWidgetTag(i);
			if (m_pGameLogic)
			{
				ccColor3B color = m_pGameLogic->getColorBtnColor(i);
				bt->setColor(color.r,color.g,color.b);
			}
			
		}
	}

	cs::CocoTextButton* bt = (cs::CocoTextButton*)(COCOUISYSTEM->getWidgetByName("btnBack"));
	bt->addReleaseEvent(this, coco_releaseselector(GameState::menuBackCallback));

	cs::CocoImageView* img = (cs::CocoImageView*)(COCOUISYSTEM->getWidgetByName("imgMainPictureBG"));
	if(img)
		m_pGameLogic->GetUIpos().picutrePos = img->getPosition();

	if(m_pGameLogic)
		m_pGameLogic->ShowLevelDesign();
}

void GameState::menuColorBtnCallback( CCObject* pSender )
{
	cs::CocoButton* bt = (cs::CocoButton*) pSender;
	if (m_pGameLogic)
	{
		m_pGameLogic->TouchEvent(bt->getWidgetTag());			
	}
}

void GameState::OnFailed()
{
	cs::CocoImageView* img = (cs::CocoImageView*)(COCOUISYSTEM->getWidgetByName(("imgFailed")));
	if (img)
	{
		img->setVisible(true);
	}
}

void GameState::OnSucceed()
{
	cs::CocoImageView* img = (cs::CocoImageView*)(COCOUISYSTEM->getWidgetByName("imgSucceed"));
	if (img)
	{
		img->setVisible(true);
		GameData::GetIns()->GetCurrentRole()->mIconIndex++;
	}
}

void GameState::OnGuessedOne()
{
	if(m_pGameLogic)
	{
		//cs::CocoLoadingBar* bt = (cs::CocoLoadingBar*)(COCOUISYSTEM->getWidgetByName("processLevel"));
		//if(bt)
		//	bt->setPercent(m_pGameLogic->GetLevelProcess()*100);
	}
}

void GameState::OnFailedOne()
{
	if(m_pGameLogic)
	{
		int lifeLeft = m_pGameLogic->GetLifeLeft();
		cs::CocoImageView* img = (cs::CocoImageView*)(COCOUISYSTEM->getWidgetByName(("life"+GetString(lifeLeft + 1)).c_str()));
		if (img)
		{
			img->setVisible(false);
		}
		
	}
}

void GameState::menuBackCallback( CCObject* pSender )
{
	if(m_pGameLogic)
	{		
		delete m_pGameLogic;
		m_pGameLogic = 0;
	}

	MainMenuUI();
}

